package com.tang.controller;

import com.tang.config.ValidGroup;
import com.tang.entity.ValidVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Email;
import java.util.ArrayList;
import java.util.List;

/**
 * @author JH
 */
@RestController
@RequestMapping(value = "/valid",produces = "application/json")
@Slf4j
@Validated
public class ValidController {

    @PostMapping("/test1")
    public String test1(@Validated @RequestBody ValidVO validVO){
        log.info("validEntity is {}",validVO);
        return "test1 valid success";
    }

    @PostMapping("/test2")
    public String test2(@Validated ValidVO validVO){
        log.info("validEntity is {}",validVO);
        return "test2 valid success";
    }

    @PostMapping("/test3")
    public String test3(@Email String email){
        log.info("email is {}",email);
        return "email valid is success";
    }

    @PostMapping("/add")
    public String add(@Validated(value = ValidGroup.Crud.Create.class) ValidVO validVO){
        log.info("valid add Entity is{}",validVO);
        return "add valid success";
    }

    @PostMapping("/update")
    public String update(@Validated(value = ValidGroup.Crud.Update.class) ValidVO validVO){
        log.info("update entity is {}",validVO);
        return "update valid success";
    }

    @GetMapping("/error1")
    public void empty(){
        throw new RuntimeException("自定义异常");
    }

    @GetMapping("/list")
    public List<String> list(){
        List<String> list = new ArrayList<>();
        list.add("张三");
        list.add("李四");
        list.add("jack");
        return list;
    }
}
